<?php

function list_karyawan_realisasi($periode_id){ 
	$periode = my_get_data_by_id('pr_periode','periode_id',$periode_id);
	$bulan = (int) date('m' , strtotime($periode['tanggal']));
	$tahun = (int) date('Y' , strtotime($periode['tanggal']));
	$end=$periode['tahun'].'-'. sprintf( '%02d',$bulan) .'-10' ;
	$header1 = array(
		'#'=>array('style'=>'border-bottom:2px solid;width:3%'),
		'nik'=>array('style'=>'border-bottom:2px solid;width:8%'),
		'nama'=>array('style'=>'border-bottom:2px solid;width:20%'), 
	);
	$header2= array();
	$tanggals = list_kalender($periode['dari'] , $periode['sampai']  );
	$tanggals = array_unique($tanggals) ;
	foreach($tanggals as $tgl){ 
		$header2[date('d',strtotime($tgl) )] = array('style'=>'border-bottom:2px solid;width:1%');
	}
	$header3 = array(' '=>array('style'=>'border-bottom:2px solid;width:3%'));
	$header = $header1 + $header2 /*+ $header3*/;
	if(isset($_GET['key'])){
		$query = "
			SELECT * FROM karyawan a 
			INNER JOIN biodata b 
			ON a.biodata_id = b.biodata_id
			WHERE a.karyawan_id NOT IN (select karyawan_id FROM karyawan_disable)	
			AND a.karyawan_id NOT IN (select karyawan_id FROM pemberhentian  WHERE TMT <= '{$end}') 	
			AND ( b.nama LIKE '%{$_GET['key']}%' OR a.nik='{$_GET['key']}')
			ORDER BY a.nik
		";
	}else{
		$query = "
			SELECT * FROM karyawan a 
			INNER JOIN biodata b 
			ON a.biodata_id = b.biodata_id
			WHERE a.karyawan_id NOT IN (select karyawan_id FROM karyawan_disable)	
			AND a.karyawan_id NOT IN (select karyawan_id FROM pemberhentian  WHERE TMT <= '{$end}') 	
			ORDER BY a.nik
		";
	}
	$result = my_query($query);
	$total_records = my_num_rows($result );
	$scroll_page =5;   
	$per_page =24;  
	$current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1 ; 
	if($current_page < 1){
		$current_page = 1;
	}		 
	$pager_url  ="index.php?com={$_GET['com']}&periode_id={$_GET['periode_id']}&task={$_GET['task']}&key={$_GET['key']}&page=";	 
	$inactive_page_tag = 'style="padding:4px;background-color:#BBBBBB"';  
	$previous_page_text = ' Mundur '; 
	$next_page_text = ' Maju ';  
	$first_page_text = ' Awal '; 
	$last_page_text = ' Akhir ';
	
	$kgPagerOBJ = new kgPager();
	$kgPagerOBJ->pager_set(
		$pager_url, 
		$total_records, 
		$scroll_page, 
		$per_page, 
		$current_page, 
		$inactive_page_tag, 
		$previous_page_text, 
		$next_page_text, 
		$first_page_text, 
		$last_page_text ,$pager_url_last); 
	 		
	$result = my_query($query ." LIMIT ".$kgPagerOBJ->start.", ".$kgPagerOBJ->per_page); 	
	$row = array(); 
	$i = ($current_page  - 1 ) * 24; 
	while($ey = my_fetch_array($result )){
		$i++;
		$karyawan =  loaddata_karyawan($ey['karyawan_id']); 
 
		$detailproperty = array(
			'href'=>'index.php?com='.$_GET['com'].'&task=list_detail&karyawan_id='.$ey['karyawan_id'], 
			'title'=>'Detail'
		);
		$detail_button = button_icon( 'b_props.png' , $detailproperty  ); 
		$row1 = array(
			'#'=>position_text_align ($i, 'center'), 
			'nik'=>position_text_align( $karyawan['nik'], 'center'),
			'nama'=>substr($karyawan['nama_gelar'], 0,12), 
		//	'wk'=>position_text_align( $karyawan['waktu_kerja'] , 'center'), 
		//	'kel'=>position_text_align( $karyawan['kelompok'] , 'center'), 
			 
			 
		);
		$row2  = array(); 
		$ds =get_realisasi_karyawan( $periode_id,$ey['karyawan_id']); 
		foreach($tanggals as $tgl){ 
				list( $yyt,$mmt,$tgldd)  = explode("-", $tgl);
				$row2[date('d',strtotime($tgl) )] =  position_text_align( $ds[$tgldd] , 'right'); 
		 
		}
		$row3   = array(' '=> position_text_align( $detail_button  , 'right'));
		$row[] = array_merge( $row1 , $row2 /* , $row3 */);
	}
	
	$datas = table_rows($row); 
	$paging = $kgPagerOBJ ->showPaging();
	$view =  form_header( "detail pegawai" , "cp"  );
	
	$query_check_periode = " SELECT * FROM pr_periode WHERE periode_id > 23 ORDER BY periode_id DESC";
	$result = my_query($query_check_periode);
	$prs = array();
	while($prow = my_fetch_array($result)){
		$prs[$prow['periode_id']] = date('Y-m',strtotime($prow['tanggal']));
	}
	$fperiode_id = array(
		'id'=>'periode_id',
		'value'=>$periode_id ,
		'name'=>'periode_id',
	);
	$view .= form_field_display( form_dropdown($fperiode_id,$prs)   , "Periode Gaji"   );
	
	$nik_start = isset($_GET['nik_start']) ? $_GET['nik_start'] : $nik_start_default;
	$nik_end = isset($_GET['nik_end']) ? $_GET['nik_end'] : $nik_end_default;
	$form_start = '<input type="text" size="7" value="'.$nik_start.'" name="nik_start" />';
	$form_end = '<input type="text" size="7" value="'.$nik_end.'" name="nik_end" />';
	$view .= '<input type="hidden"  value="'.$_GET['com'].'" name="com" />';
	//$view .= form_field_display( $form_start.' s/d '.$form_end  , "NIK Range"   );
	
	$submit = array(
		'value' => (  ' Check '  ),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 	
	$cetak = array(
		'value' => (  ' Cetak '  ),
		'onclick' => 'javascript:location.href=\'index.php?com='.$_GET['com'].'&periode_id='.$periode_id.'&task=excel&nik_start='.$_GET['nik_start'].'&nik_end='.$_GET['nik_end'].'\'', 
		'type'=>'button','class'=>'main_button'
	);
	$form_cetak= form_dynamic($cetak); 	
 
	$excel = array(
		'value' => (  ' Excel '  ),
		'onclick' => 'javascript:location.href=\'index.php?com='.$_GET['com'].'&periode_id='.$periode_id.'&task=excel&nik_start='.$_GET['nik_start'].'&nik_end='.$_GET['nik_end'].'\'', 
		'type'=>'button','class'=>'main_button'
	);
	$form_excel= form_dynamic($excel); 
	
	$view .= form_field_display(  $form_submit .' '.$form_excel , "&nbsp;" ,  "" );
	
	$view .= form_footer( );	
	$view = str_replace("method=\"post\"","method=\"get\"",$view);
	  
	return  $view. table_builder($header , $datas ,  15 ,false ,$paging );
}


function get_realisasi_hadir($date , $karyawan_id){
	list($yyyy,$mm,$dd) = explode("-" , $date);
	$dd = (int) $dd;
	$query = "SELECT status_absen FROM  tmn_hari_realisasi
		WHERE karyawan_id = {$karyawan_id} AND tgl_index = '{$dd}' ";
	$result = my_query($query);
	$row = my_fetch_array($result);
	return $row['status_absen'];
}

function get_realisasi_karyawan(  $periode_id , $karyawan_id){
	list($yyyy,$mm,$dd) = explode("-" , $date);
	$dd = (int) $dd;
	$query = "SELECT tgl_index , status_absen FROM  tmn_hari_realisasi
		WHERE  periode_id={$periode_id} AND karyawan_id = {$karyawan_id}";
	$result = my_query($query);
	$datas = array();
	while($row = my_fetch_array($result)){
		$dx = sprintf('%02d',$row['tgl_index']);
		$datas[$dx] = $row['status_absen'];
	}
	return $datas;
}

function get_kelompok_jadwal_by_upload(   $karyawan_id ){
	$query = "SELECT * FROM tmn_jadwal_karyawan WHERE karyawan_id = {$karyawan_id}";
	$result = my_query($query);
	$row = my_fetch_array($result);
	return $row;
}
  

function excel_hari_kerja($periode_id){
	$periode = my_get_data_by_id('pr_periode','periode_id',$periode_id);
	$bulan = (int) date('m' , strtotime($periode['tanggal']));
	$tahun = (int) date('Y' , strtotime($periode['tanggal']));
	$end=$periode['tahun'].'-'. sprintf( '%02d',$bulan) .'-10' ;
	$header1 = array( 
		'nik'=>array('style'=>'border-bottom:2px solid;width:8%'),
		'nama'=>array('style'=>'border-bottom:2px solid;width:20%'), 
	);
	$header2= array();
	$tanggals = list_kalender($periode['dari'] , $periode['sampai']  );
	$tanggals = array_unique($tanggals) ;
	foreach($tanggals as $tgl){ 
		$header2[date('d',strtotime($tgl) )] = array('style'=>'border-bottom:2px solid;width:1%');
	}
	$header3 = array(' '=>array('style'=>'border-bottom:2px solid;width:3%'));
	$header = $header1 + $header2;
	
	$query = "SELECT * FROM karyawan a 
			INNER JOIN biodata b 
			ON a.biodata_id = b.biodata_id
			WHERE a.karyawan_id NOT IN (select karyawan_id FROM karyawan_disable)	
			AND a.karyawan_id NOT IN (select karyawan_id FROM pemberhentian  WHERE TMT <= '{$end}') 	
			ORDER BY a.nik
			"; 
	$result = my_query($query);
	$row = array(); 
	while($ey = my_fetch_array($result )){
	 	$karyawan =  loaddata_karyawan($ey['karyawan_id']); 
 
		$detailproperty = array(
			'href'=>'index.php?com='.$_GET['com'].'&task=list_detail&karyawan_id='.$ey['karyawan_id'], 
			'title'=>'Detail'
		);
		$detail_button = button_icon( 'b_props.png' , $detailproperty  ); 
		$row1 = array( 
			'nik'=> "'".$karyawan['nik'],  
			'nama'=> $karyawan['nama'],  
			 
		);
		$row2  = array(); 
		$ds =get_realisasi_karyawan( $periode_id , $ey['karyawan_id']); 
		foreach($tanggals as $tgl){ 
				list( $yyt,$mmt,$tgldd)  = explode("-", $tgl);
				$row2[date('d',strtotime($tgl) )] =    $ds[$tgldd]  ; 
		 
		}
		$row3   = array(' '=> position_text_align( $detail_button  , 'right'));
		$row[] = array_merge( $row1 , $row2 /* , $row3 */);
	}
	
	$datas = table_rows_excel($row); 
	return table_builder_excel($header , $datas ,4 ,false ); 	
}
